package com.copsoftware.comun.services;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.copsoftware.comun.dao.GeometryDAO;
import com.copsoftware.hibernate.cfg.HibernateUtils;
import com.copsoftware.reportes.geografico.beans.BeanDelitoFlt;

public class GeometryService {
	protected final Log logger = LogFactory.getLog(getClass());

	public void guardarGeometry(String cadenaPuntos) throws Exception {
		Session session = HibernateUtils.getSessionFactory()
				.getCurrentSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			GeometryDAO geometryDAO = new GeometryDAO();
			geometryDAO.setSession(session);
			logger.info("Guardando Geometry");
			// geometryDAO.guardarGeometryPoints(cadenaPuntos);
//			geometryDAO.guardarGeometryLugar(cadenaPuntos);
			geometryDAO.guardarZona(cadenaPuntos);
			logger.info("Guardo Geometry");
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			throw e;
		}
	}

	public List buscarZona() throws Exception {
		Session session = HibernateUtils.getSessionFactory()
				.getCurrentSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			GeometryDAO geometryDAO = new GeometryDAO();
			geometryDAO.setSession(session);
			List listaZonas = geometryDAO.buscarZona();
			tx.commit();
			return listaZonas;
		} catch (Exception e) {
			tx.rollback();
			throw e;
		}
	}

	public List buscarDelitos(BeanDelitoFlt beanDelitoFlt) throws Exception {
		Session session = HibernateUtils.getSessionFactory()
				.getCurrentSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			GeometryDAO geometryDAO = new GeometryDAO();
			geometryDAO.setSession(session);
			List listaDelitos = geometryDAO.buscarDelitos(beanDelitoFlt);
			logger.info("La lista delitos tiene " + listaDelitos.size()
					+ " elementos");
			tx.commit();
			return listaDelitos;
		} catch (Exception e) {
			tx.rollback();
			e.printStackTrace();
			throw e;
		}
	}

	public String obtenerGLatLng(Integer idDenuncia) throws Exception {
		Session session = HibernateUtils.getSessionFactory()
				.getCurrentSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			GeometryDAO geometryDAO = new GeometryDAO();
			geometryDAO.setSession(session);
			String wkt = geometryDAO.obtenerGLatLng(idDenuncia);
			logger.info("Busco idDenuncia " + idDenuncia);
			tx.commit();
			return wkt;
		} catch (Exception e) {
			tx.rollback();
			e.printStackTrace();
			throw e;
		}
	}

	public String[] obtenerGPolygonCalle(String deCalle) throws Exception {
		Session session = HibernateUtils.getSessionFactory()
				.getCurrentSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			GeometryDAO geometryDAO = new GeometryDAO();
			geometryDAO.setSession(session);
			String [] params = geometryDAO.obtenerGPolygonCalle(deCalle);
			logger.info("Busco idCalle " + deCalle);
			tx.commit();
			return params;
		} catch (Exception e) {
			tx.rollback();
			e.printStackTrace();
			throw e;
		}
	}

	public static void main(String[] args) throws Exception {
		GeometryService geometryService = new GeometryService();
		BeanDelitoFlt bnBeanDelitoFlt = new BeanDelitoFlt();
		bnBeanDelitoFlt.setTipoDelito("1");
		bnBeanDelitoFlt.setEstado("1");
		bnBeanDelitoFlt.setFechaInicial("12/01/2007");
		bnBeanDelitoFlt.setFechaFinal("19/12/2008");
		System.out.println(geometryService.buscarDelitos(bnBeanDelitoFlt)
				.size());
	}

}
